<?php

/**
 * @author nhatnam
 * @copyright 2011
 */
//tao function ket noi
include('clsketnoi.php');

function taomaphieuthutienphat() 
{ 
    return taomatudong("select mapttp from tblphieuthutienphat","mapttp", 4, 15 );
} 
//them du lieu
function themphieuthutienphat()
{
    $mapttp = $_POST['mapttp'];
    $manv = $_POST['nhanvienlap']; //function hienthinhanvien value = manv
    $sotien = $_POST['sotien'];
    $madg = $_POST['docgia'];
    
    $query = mysql_query("insert into tblphieuthutienphat (mapttp, manv, madg, sotien) values ('$mapttp','$manv','$madg','$sotien')",dbketnoi());
    mysql_free_result($query);
}
//xoa 
function xoaphieuthutienphat()
{
    $mapttp= $_POST["mapttp"];
   
    mysql_query("delete from tblphieuthutienphat where mapttp='$mapttp'",dbketnoi());
    mysql_free_result();
  
}
//update 
function updatephieuthutienphat()
{
    $mapttp = $_POST['mapttp'];
    $manv = $_POST['nhanvienlap']; //function hienthinhanvien value = manv
    $sotien = $_POST['sotien'];
    $madg = $_POST['docgia'];
    
    $query = mysql_query("update tblphieuthutienphat set manv='$manv', madg='$madg', sotien='$sotien' where mapttp='$mapttp'",dbketnoi());
    mysql_free_result($query);
}

// hien thi du lieu
function hienthiphieuthutienphat()
{
    $limit = 15; //gioi han so row trong 1 trang

    $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1; //get trang hien thi

    $page_start = ( $page - 1 ) * $limit;
    $page_end = $page * $limit;

    $query = mysql_query("SELECT mapttp, sotien, tbldocgia.hoten as tendocgia, tblnhanvien.hoten as tennhanvien from tblphieuthutienphat, tbldocgia, tblnhanvien".
    " where tblphieuthutienphat.madg=tbldocgia.madg and tblphieuthutienphat.manv=tblnhanvien.manv",dbketnoi());

    $number_of_page = ceil(mysql_num_rows($query)/$limit); // lay tong row

    if ($number_of_page > 1)
    {
        $list_page = " <td> Trang: </td>";

        for ( $i = 1; $i <= $number_of_page; $i++ )
            {
            if ( $i == $page )
            {
                $list_page .= " <td>[ <b>{$i}</b> ]</td> ";
           
            }
            else
            {
                $list_page .= "<td><a href='giaodienphieuthutienphat.php?page={$i}'> {$i} </a></td>";
                
            }
        }
    }
    taotable();
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        if ( $i >= $page_start )
        {
            //mau bg cho dong chan le
            if($i%2==0)
                $bg = "#d0eafb";
            else
                $bg = "#66aee0";
            
            //tao ra 1 table bang lenh ket hop css
            echo "<tr bgcolor='".$bg."'>"; 
            echo '<td><a href="giaodienphieuthutienphat.php">'.$result['mapttp'].'</a></td>';
            echo "<td>".$result['sotien']."</td>";
            echo "<td>".$result['tendocgia']."</td>";
            echo "<td>".$result['tennhanvien']."</td>";
            echo "</tr>";
        }

        $i++;

        if ($i >= $page_end)
        {
            break;
        }
    }
    echo "</table></br>";
    echo $list_page;
}
function taotable()
{
    $title = array("Mã PTTP ","Số Tiền","Của Độc Giả","Nhân Viên Lập");
    echo "<table style='border-color:black;'  cellspacing='1' width='670'>";
    echo "<tr>";
    for($i=0; $i<=3; $i++)
    {
        echo "<th style='background-color:#2287cd; color='white';>".$title[$i]."</th>"; 
    }
    echo "</tr>";
  
}

function hienthinhanvien()
{
    $query = mysql_query("select manv, hoten, bophan from tblnhanvien where bophan='Thủ kho'",dbketnoi());
    $i=0;
    echo "<select size='1' name='nhanvienlap'>";
    echo "<option value=''>----Chọn----</option>";
    while($result = mysql_fetch_array($query) )
    {
        echo "<option value='".$result['manv']."'>".$result['hoten']."</option>";
        $i++;
    }
    echo "</select>";
}

function hienthidocgia()
{
    $query = mysql_query("select madg, hoten from tbldocgia group by hoten",dbketnoi());
    $i=0;
    echo "<select size='1' name='docgia'>";
    echo "<option value=''>----Chọn----</option>";
    while($result = mysql_fetch_array($query) )
    {
        echo "<option value='".$result['madg']."'>".$result['hoten']."</option>";
        $i++;
    }
    echo "</select>";
}

?>
